package com.guogee.pdb.db;

import java.util.ArrayList;
import java.util.List;

import com.guogee.pdb.utils.BuildHelper;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
public class DBHelper {

	private static DBHelper mInstance;
	public static final String DB_NAME = "pdb";
	
	private static final int MONGO_SERVER_PORT1 = 2217;

	private MongoClient mongo;
	private DB ismartDB;

	public static final  String USERNAME = "pdb";
	public static final  String PASSWORD = "Pt09clA56Vm";
	
	public DBHelper() {
		// TODO Auto-generated constructor stub
		mInstance = new DBHelper("127.0.0.1");
	}

	public DBHelper(String ip) {
		// 构建Seed列表
        List<ServerAddress> seedList = new ArrayList<ServerAddress>();
	    ServerAddress seed1 = new ServerAddress(ip, MONGO_SERVER_PORT1);
        seedList.add(seed1);
        // 构建鉴权信息
        String dbname = DB_NAME;
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(MongoCredential.createScramSha1Credential(USERNAME,
                dbname, PASSWORD.toCharArray()));
        
        mongo = new MongoClient(seedList, credentials);
        ismartDB = mongo.getDB(dbname); 
	
	};

	public static DBHelper getInstance() {
		return mInstance;
	}
	
	public static void initDB(String ip){
		mInstance = new DBHelper(ip);
	}

	public Mongo getMongo() {
		return mongo;
	}

	public DB getIsmartDB() {
		return ismartDB;
	}

	/**
	 * 经销商表
	 * @author huanghuan1
	 *
	 */
	public static class AgencyCollection {
		public static final String COLLECTION_NAME = "Agency";
		public static final String ID = "_id";
		public static final String UUID = "uuid";
		public static final String COMPANY_NAME = "companyName"; //公司名称
		public static final String AGENT = "agent"; //代理人
		public static final String PHONE = "mobile";
		public static final String EMAIL = "email";
		public static final String REG_TIME = "regTime";
	}
	
	/**
	 * 小区表
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class CommunityCollection {
		public static final String COLLECTION_NAME = "Community";
		public static final String ID = "_id";
		public static final String UUID = "uuid"; // 小区id
		public static final String NAME = "name"; // 小区名字
		public static final String COUNTRY = "country"; // 国家
		public static final String PROVINCE = "province"; // 省
		public static final String CITY = "city"; // 市
		public static final String REGION = "region"; // 区
		public static final String AGENT_ID = "agentId"; //代理商id

	}
	
	
	/**
	 * 小区维护人员数量
	 */
	public static class CommunityAccendantCollection {
		public static final String COLLECTION_NAME = "CommunityAccendant";
		public static final String ID = "_id";
		public static final String COMMUNITY_ID = "communityId";
		public static final String PEOPLE_COUNT = "count";
	}
	
	
	
	/**
	 * 网关与经销商的关系表
	 * @author huanghuan1
	 *
	 */
	public static class RLGatewayAgencyCollection {
		public static final String COLLECTION_NAME = "RL_Gateway_Agency";
		public static final String ID = "_id";
		public static final String MAC = "mac";
		public static final String AGENT_ID = "agentId";
		public static final String ENABLE = "enable";
	}
	
	/**
	 * 用户表
	 * @author huanghuan1
	 *
	 */
	public static class UserCollection {
		public static final String COLLECTION_NAME = "Users";
		public static final String ID = "_id";
		public static final String USER_NAME = "UserName";
		public static final String USER_PWD = "UserPwd";
		public static final String EMAIL = "Email";
		public static final String PHONE = "MobileNo";
		public static final String USER_LEVEL = "UserLevel";
		public static final String FIRST_LOGIN = "FirstLoginTime";
		public static final String LAST_LOGIN = "LastLoginTime";
		public static final String ALLOW_LOGIN = "AllowLogin";
		public static final String USER_TYPE = "userType";
		public static final String THIRD_USERID = "thirdUserId"; // 第三方用户的id
		public static final String REAL_NAME = "realName"; // 真实姓名
		public static final String COMMUNITY_ID = "communityId"; // 小区id
	}
	
	//第三方用户配置表
	public static class UserThirdConfigCollection {
		public static final String COLLECTION_NAME = "UserThirdConfig";
		public static final String ID = "_id";
		public static final String USER_ID = "userId";
		public static final String THIRD_USER_ID = "thirdUserId";
		public static final String THIRD_TYPE = "thirdType";
		public static final String THIRD_NICKNAME = "thirdNickname";
	}
	
	
	/**
	 * 家庭表
	 */
	public static class FamilyCollection {
		public static final String COLLECTION_NAME = "Family";
		public static final String ID = "_id";
		public static final String NAME = "name";
		public static final String CREATOR = "creator";
		public static final String MOBLIE = "moblie";
		public static final String PASSWORD = "password";
		public static final String ADDR = "addr";
		public static final String COMMUNITY_ID = "communityId";
		public static final String CREATE_TIME = "createTime";
		public static final String SERVICE_ID = "serviceId";
	}
	
	/**
	 * 服务表
	 */
	public static class ServicesCollection {
		public static final String COLLECTION_NAME = "Services";
		public static final String ID = "_id";
		public static final String NAME = "name";
		public static final String DURATION = "duration";
		public static final String ACTIVATE_TIME = "activateTime";
	}

	/**
	 * 家庭成员表
	 */
	public static class FamilyMembersCollection {
		public static final String COLLECTION_NAME = "FamilyMembers";
		public static final String ID = "_id";
		public static final String FAMILY_ID = "familyId";
		public static final String MEMBER_ID = "memberId";
		public static final String ADDED_TIME = "addedTime";
	}

	/**
	 * 家庭设备表
	 */
	public static class FamilyDevicesCollection {
		public static final String COLLECTION_NAME = "FamilyDevices";
		public static final String ID = "_id";
		public static final String FAMILY_ID = "familyId";
		public static final String DEV_ID = "devId";
		public static final String DEV_TYPE = "devType";
		public static final String DEV_VERSION = "devVersion";
		public static final String DEV_MAC = "devMac";
	}
	
	/**
	 * 家庭设备历史表
	 */
	public static class FamilyDevicesHistoryCollection {
		public static final String COLLECTION_NAME = "FamilyDevicesHistory";
		public static final String ID = "_id";
		public static final String FAMILY_ID = "familyId";
		public static final String DEV_MAC = "devMac";
		public static final String DEV_TYPE = "devType";
		public static final String DEV_VERSION = "devVersion";
		public static final String BIND_TIME = "bindTime";
	}
	

	/**
	 * 网关与设备关系表
	 */
	public static class RLGatewayDeviceCollection {
		public static final String COLLECTION_NAME = "RLGatewayDevice";
		public static final String ID = "_id";
		public static final String GW_SN = "gwSn";
		public static final String DEV_MAC = "devMac";
		public static final String BIND_TIME = "bindTime";
		public static final String DEV_TYPE = "devType"; //设备类型
	}


	/**
	 *门锁用户表
	 * @author huanghuan1
	 *
	 */
	public static class SmartLockCollection {
		public static final String COLLECTION_NAME = "SmartLock";
		public static final String ID = "_id";
		public static final String DEVICE_ADDR = "deviceAddr";
		public static final String LOCK_ID = "lockId";
		public static final String USER_NAME = "userName";
	}



	/**
	 *  智能锁开锁记录
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class UnlockedRecordCollection {
		public static final String COLLECTION_NAME = "UnlockedRecord";
		public static final String ID = "_id";
		public static final String DEVICE_ADDR = "deviceAddr"; // 锁的地址
		public static final String OPEN_TIME = "openTime"; // 开锁时间
		public static final String USER_ID = "userId"; // 开锁用户id
		public static final String OPEN_METHOD = "openMethod"; // 开锁方式
		public static final String INVISIBLE = "invisible"; //是否可见
	}

	/**
	 * 报警记录
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class AlarmRecordCollection {
		public static final String COLLECTION_NAME = "AlarmRecord";
		public static final String ID = "_id";
		public static final String DEVICE_ADDR = "deviceAddr"; // 锁的地址
		public static final String DEVICE_TYPE = "deviceType"; // 设备类型
		public static final String ALARM_TYPE = "alarmType"; // 报警类型
		public static final String DATA = "data"; // 其它数据内容，比如用户id 胁迫报警有用
		public static final String ALARM_TIME = "alarmTime"; // 报警时间
		public static final String RESOLVED = "resolved"; // 报警是否已解决
		public static final String RESOLVED_TIME = "resolvedTime"; // 报警解除时间
		public static final String TROUBLE_ID = "troubleId"; // 故障单Id，推送到保安的才有该值
		public static final String ALARM_ADDR = "alarmAddr"; // 报警地址
		public static final String COMMUNITY_ID = "communityId"; // 报警地址所在的小区id
		public static final String OWNER_PHONE = "ownerphone"; // 业主电话
		public static final String HAD_NOTIFY_SECURITY = "notifySecurity"; //是否通知保安
		public static final String INVISIBLE = "invisible"; //是否可见
	}
	
	/**
	 * 报警处理进度表
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class AlarmProcessCollection {
		public static final String COLLECTION_NAME = "AlarmProcess";
		public static final String ID = "_id";
		public static final String ALARM_ID = "alarmId"; // 报警id
		public static final String TROUBLE_ID = "troubleId"; // 故障单Id，推送到保安的才有该值
		public static final String STEP = "step"; // 最新处理进度
		public static final String OVERVIEW = "overView"; // 处理情况概述
		public static final String DETAIL = "detail"; // 处理详情
		public static final String PROCESS_TIME = "processTime"; // 处理时间
		public static final String IMG_URL = "imgUrl"; // 图片的url
		public static final String STATUS = "status"; // 状态 0:未知 1:成功 -1:失败
	}
	

	/**
	 * 故障单详情
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class TroubleTicketCollection {
		public static final String COLLECTION_NAME = "TroubleTicket";
		public static final String ID = "_id";
		public static final String TROUBLE_ID = "troubleId"; // 故障单id
		public static final String ALARM_TYPE = "alarmType"; // 报警类型
		public static final String ALARM_ADDR = "alarmAddr"; // 报警地址
		public static final String ACCEPTOR = "acceptor"; // 受理人
		public static final String ACCEPT_MOBILE = "acceptMobile"; // 受理手机号码
		public static final String ACCEPT_TIME = "acceptTime"; // 受理时间
		public static final String BEFORE_URL = "before_url"; // 处理前的url
		public static final String BEFORE_TEXT = "before_text"; // 处理前的文本
		public static final String AFTER_URL = "after_url"; // 处理后的url
		public static final String AFTER_TEXT = "after_text"; // 处理后的文本
		public static final String EVALUATE_TIME = "evaluateTime"; // 评价时间
		public static final String CREATE_TIME = "createTime"; // 创建时间
		public static final String FINISH_TIME = "finishTime"; // 结束时间
	}

	/**
	 * 警情订单评价
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class EvaluationCollection {
		public static final String COLLECTION_NAME = "Evaluation";
		public static final String ID = "_id";
		public static final String TROUBLE_ID = "troubleId"; // 故障单id
		public static final String QUALITY = "quality"; // 处理质量
		public static final String EVALUATION = "evaluation"; // 评价
		public static final String CREATE_TIME = "createTime"; // 创建时间
	}
	

	/**
	 *  授权记录
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class LockAuthRecordCollection {
		public static final String COLLECTION_NAME = "LockAuthRecord";
		public static final String ID = "_id";
		public static final String DEVICE_ADDR = "deviceAddr"; // 锁的地址
		public static final String METHOD = "method"; // 授权方式 0：单次授权 1:期限授权 2:永久授权
		public static final String EFFECT_TIME = "effectTime"; // 生效时间
		public static final String EXPIRE_TIME = "expireTime"; // 失效时间
		public static final String PASSWORD = "password"; // 密码
		public static final String STATUS = "status"; // 0:生成成功
		public static final String CREATE_TIME = "createTime"; // 创建时间
		public static final String REMARK_NAME = "mark"; // 备注
	}
	
	/**
	 * 事件表
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class EventCollection {
		public static final String COLLECTION_NAME = "Event";
		public static final String ID = "_id";
		public static final String EVENT_ID = "eventId"; // 事件id
		public static final String DEVICE_ADDR = "deviceAddr"; // 设备地址
		public static final String EVENT_TYPE = "eventType"; // 事件类型
		public static final String EVENT_LOC = "eventLoc"; // 事件发生地址
		public static final String EVENT_COMMUNITY = "eventCommunity"; // 事件发生地址所在的小区
		public static final String EVENT_TIME = "eventTime"; // 事件时间
		public static final String EXPIRE_TIME = "expireTime"; // 失效时间
		public static final String EVENT_STATUS = "eventStatus"; // 事件状态
	}
	
	
	/**
	 * 验证码表
	 * @author huanghuan1
	 *
	 */
	public static class AuthCodeCollection{
		public static final String COLLECTION_NAME = "AuthCode";
		public static final String ID = "_id";
		public static final String MOBILE = "mobile"; //手机号码
		public static final String AUTHCODE = "authCode"; //验证码
		public static final String CREATE_TIME = "createTime"; //生成时间
		public static final String TYPE = "type"; //验证码类型 0:修改密码 1:保安
		
	}
	

	/***
	 * 短信发送记录表
	 * 
	 * @author huanghuan1
	 *
	 */
	public static class SMSSendHistoryCollection {
		public static final String COLLECTION_NAME = "SMSSendHistory";
		public static final String ID = "_id";
		public static final String MOBILE = "mobile"; // 手机号码
		public static final String CONTENT = "content"; // 短信内容
		public static final String SEND_TIME = "sendTime"; // 发送时间
		public static final String STATUS = "status"; // 发送结果 0:成功 1:失败
	}

	
	/**
	 * 用户上线记录
	 */
	public static class UserLoginRecordCollection {
		public static final String COLLECTION_NAME = "UserLoginRecord";
		public static final String ID = "_id";
		public static final String USER_ID = "userId"; // 用户id
		public static final String LOGIN_TIME = "loginTime"; // 上线时间
	}
	
	/**
	 * 门铃设备推送信息表
	 */
	public static class DoorBellInfoCollection {
		public static final String COLLECTION_NAME = "DoorbellRecord";
		public static final String ID = "_id";
		public static final String DEVICE_TYPE = "deviceType";
		public static final String MAC = "mac";
		public static final String FUN = "fun";
		public static final String PATH = "path";
		public static final String TIME = "time";
		public static final String DATA = "data";
		public static final String INVISIBLE = "invisible"; //是否可见
	}

}
